<?php
General::getTpl()->assign('extraTitle',"Seitenboxen");

if(!isset($_GET['pageId']) || !is_numeric($_GET['pageId'])) exit;
General::getTpl()->assign('pageId',$_GET['pageId']);

if(!Admin::checkPermission('canManagePages')) {
	General::ouputMessage('Fehler','Sie haben nicht gen&uuml;gend Rechte um diese Seite zu betrachten');
}else{
	
	/**
	 * NEW CATEGORIE
	 */
	if(isset($_GET['do']) && $_GET['do'] == "new") {
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = "";
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		
		$form = new Form();
		$form->initByData("Box erstellen",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			
			// Create Box
			Pages::createBox($_GET['pageId'],$_POST['Name']);
			General::ouputMessage('Erfolgreich','Die Box wurde erfolgreich gespeichert.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	

	
	if(isset($_GET['do']) && $_GET['do'] == "edit" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		// Edit Navigation?
		// Get Data
		$query = DB::getInstance()->query("
			SELECT
				pageId,
				boxName
			FROM
				".Tables::$pageBoxTbl."
			WHERE
				id = '".DB::parseVar($_GET['id'])."'");
		$data = DB::getInstance()->fetchArray($query);
		
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = $data['boxName'];
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		// Navigation
		$pages = Pages::getPages();
		$keyString = "";
		$valueString = "";
		foreach($pages as $p) {
			$keyString .= $p['id']."::--::";
			$valueString .= $p['pageName']."::--::";
		}
		$keyString = substr($keyString,0,strlen($keyString)-6);
		$valueString = substr($valueString,0,strlen($valueString)-6);
		
		$formField[2]['fieldName'] = "Seite";
		$formField[2]['fieldType'] = Form::$TYPE_SELECT;
		$formField[2]['fieldDefaultValue'] = $data['pageId'];
		$formField[2]['fieldCheck'] = false;
		$formField[2]['fieldSelectOptions'] = $valueString;
		$formField[2]['fieldSelectKey'] = $keyString;
		$formField[2]['fieldSelectMultiple'] = false;
		
		
		
		
		
		
		
		$form = new Form();
		$form->initByData("Box bearbeiten",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			Pages::editBox($_GET['id'],$_POST['Seite'][0],$_POST['Name']);
			General::ouputMessage('Erfolgreich','Die Box wurde erfolgreich bearbeitet.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	

	
	if(isset($_GET['do']) && $_GET['do'] == "del" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		require_once './kernel/formCheck.php';
		$form = new FormCheck("Bist du sicher?","L&ouml;schen");
		// Done?
		if($form->completeCheck()) {
			if($form->isChecked()) {
				// Delete navigation
				Pages::deleteBox($_GET['id']);
			}
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	

	
	if(isset($_GET['do']) && $_GET['do'] == "up" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		$unsortedBoxes = Pages::getBoxesByPageId($_GET['pageId']);
		// Create Array
		$unsortedArray = array();
		foreach($unsortedBoxes as $data) {
			$unsortedArray[] = $data['id'];
		}
		
		$sortedArray = General::orderArray($unsortedArray,"up",$_GET['id']);
		if($sortedArray == false) {
			General::ouputMessage('Fehler','Die Box ist schon ganz oben.','form');
		}else{
			// Save
			foreach($sortedArray as $orderId=>$id) {
				DB::getInstance()->query("
					UPDATE
						".Tables::$pageBoxTbl."
					SET
						orderId = '".$orderId."'
					WHERE
						id = '".$id."'");
			}
			General::ouputMessage('Erfolgreich','Die Box wurde erfolgreich verschoben.','form');
		}
	}
	
	if(isset($_GET['do']) && $_GET['do'] == "down" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		$unsortedBoxes = Pages::getBoxesByPageId($_GET['pageId']);
		// Create Array
		$unsortedArray = array();
		foreach($unsortedBoxes as $data) {
			$unsortedArray[] = $data['id'];
		}
		$sortedArray = General::orderArray($unsortedArray,"down",$_GET['id']);
	
		if($sortedArray == false) {
			General::ouputMessage('Fehler','Die Box ist schon ganz unten.','form');
		}else{
			// Save
			foreach($sortedArray as $orderId=>$id) {
				DB::getInstance()->query("
					UPDATE
						".Tables::$pageBoxTbl."
					SET
						orderId = '".$orderId."'
					WHERE
						id = '".$id."'");
			}
			General::ouputMessage('Erfolgreich','Die Box wurde erfolgreich verschoben.','form');
		}
	}
	
	
	
	
	General::getTpl()->assign('boxes',Pages::getBoxesByPageId($_GET['pageId']));
	
	General::getTpl()->assign('body',
		General::getTpl()->fetch('../admin/pageBoxes.html'));
}
?>